<template>
    <view class="container">
        <image class="bg-image" src="/static/image.jpg" mode="aspectFill"></image>
		<view class="nav-bar">
		  <view class="back-btn" @click="goBack">
		    <image src="/static/return.png" mode="aspectFit" class="back-icon"/>
		  </view>
		 <text class="nav-title">修改密码</text>
		</view>
        
        <view class="content">
                <view class="card-body">
                    <view class="password-container">
                        <input
                            :type="showOldPassword ? 'text' : 'password'"
                            v-model="oldPassword"
                            placeholder="请输入原密码"
                            class="password-input"
                        />
                        <image
                            :src="showOldPassword ? '/static/btn_hide_login.png' : '/static/btn_visible_login.png'"
                            class="password-icon"
                            @click="showOldPassword = !showOldPassword"
                            mode="aspectFit"
                        />
                    </view>

                    <view class="password-container">
                        <input
                            :type="showNewPassword ? 'text' : 'password'"
                            v-model="newPassword"
                            placeholder="请输入新密码"
                            class="password-input"
                        />
                        <image
                            :src="showNewPassword ? '/static/btn_hide_login.png' : '/static/btn_visible_login.png'"
                            class="password-icon"
                            @click="showNewPassword = !showNewPassword"
                            mode="aspectFit"
                        />
                    </view>

                    <view class="password-container">
                        <input
                            :type="showConfirmPassword ? 'text' : 'password'"
                            v-model="confirmPassword"
                            placeholder="请确认新密码"
                            class="password-input"
                        />
                        <image
                            :src="showConfirmPassword ? '/static/btn_hide_login.png' : '/static/btn_visible_login.png'"
                            class="password-icon"
                            @click="showConfirmPassword = !showConfirmPassword"
                            mode="aspectFit"
                        />
                    </view>

                    <button class="confirm-btn" @click="handleChangePassword">确认修改</button>
         
                 </view>
             </view>
         </view>
</template>

<script>
import { modifyPassword } from '@/api/user.js'

export default {
    data() {
        return {
            oldPassword: '',
            newPassword: '',
            confirmPassword: '',
            showOldPassword: false,
            showNewPassword: false,
            showConfirmPassword: false
        }
    },
    methods: {
        async handleChangePassword() {
            if (!this.oldPassword || !this.newPassword || !this.confirmPassword) {
                uni.showToast({
                    title: '请填写完整信息',
                    icon: 'none'
                })
                return
            }

            if (this.newPassword !== this.confirmPassword) {
                uni.showToast({
                    title: '两次密码输入不一致',
                    icon: 'none'
                })
                return
            }

            try {
                const res = await modifyPassword({
                    old_password: this.oldPassword,
                    new_password: this.newPassword
                })

                if (res.data.code = 2000) {
                    uni.showToast({
                        title: '修改成功',
                        icon: 'success'
                    })
                    setTimeout(() => {
                        this.goBack()
                    }, 1500)
                } else {
                    uni.showToast({
                        title: res.message || '修改失败',
                        icon: 'none'
                    })
                }
            } catch (error) {
                console.error('修改密码失败:', error)
                uni.showToast({
                    title: '修改失败，请重试',
                    icon: 'none'
                })
            }
        },
       
     goBack() {
         uni.navigateBack();
     },
     
        
    }
}
</script>

<style>
.container {
    position: relative;
    min-height: 100vh;
}

.bg-image {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: -1;
}

.content {
    padding: 20px;
    min-height: 100vh;
}

.password-card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    margin-top: 40px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    border-bottom: 1px solid #eee;
    padding-bottom: 10px;
}

.title {
    font-size: 18px;
    color: #333;
}

.close-btn {
    font-size: 24px;
    color: #999;
    padding: 5px;
}

.password-input {
    width: 90%;
    height: 44px;
    border: 1px solid #eee;
    border-radius: 8px;
    margin-bottom: 15px;
    padding: 0 15px;
    font-size: 14px;
}

.confirm-btn {
    background: rgba(107, 75, 138, 0.9);
    color: white;
    border-radius: 8px;
    height: 44px;
    line-height: 44px;
    text-align: center;
    font-size: 16px;
    width: 100%;
    margin-top: 20px;
}

.nav-bar {
  display: flex;
  align-items: center;
  padding: 10px 20px;
  margin-bottom: 20px;
}

.back-btn {
  padding: 30px;
  margin-left: -50px;
}

.back-icon {
  width: 30px;
  height: 30px;
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 18px;
  font-weight: 500;
  margin-right: 40px;
  color: #333;
}

.password-container {
    position: relative;
    width: 100%;
    margin-bottom: 15px;
}

.password-input {
    width: 100%;
    height: 44px;
    border: 1px solid #eee;
    border-radius: 8px;
    padding: 0 40px 0 15px; /* 右侧留出图标空间 */
    font-size: 14px;
    background: rgba(255, 255, 255, 0.1);
}

.password-icon {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    z-index: 1;
}

.confirm-btn {
    background: rgba(107, 75, 138, 0.9);
    color: white;
    border-radius: 8px;
    height: 44px;
    line-height: 44px;
    text-align: center;
    font-size: 16px;
    width: 100%;
    margin-top: 20px;
}
</style> 